<?php
namespace app\admin\controller;
use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;
use app\admin\model\HetongModel;
use app\admin\model\AttendanceModel;
use app\admin\model\AccessModel;//权限查询
use think\Db;
use tree\Tree;
use think\Request;


// 系统管理
class SystemController extends AdminBaseController
{
	//
	public function index(){
		return $this->fetch();
	}
	
	public function subsystem()
	{
		$list = Db::name('system_update_record')->order('add_time DESC');
		if( cmf_get_current_admin_id() != 1 ){
			$list = $list->where('status',1);
		}
		$list = $list->paginate(20);
		$this->assign("list" , $list);
		return $this->fetch("subsystem");
	}
	
	public function downloadFile($id){
		$find = Db::name('system_update_record')->where('id',$id)->find();
		$filename = ROOT_PATH.'public/'.$find['source'];
		$title = $find['name'];
		$file  =  fopen($filename, "rb");
        Header( "Content-type:  application/octet-stream ");
        Header( "Accept-Ranges:  bytes ");
        Header( "Content-Disposition:  attachment;  filename= $title");
        while (!feof($file)) {
            echo fread($file, 8192);
            ob_flush();
            flush();
        }
        fclose($file);
	}
	
	public function modifyStatus( $id , $code )
	{
	
		$res = Db::name( 'system_update_record' )->where( 'id' , $id )->update( [ 'status' => $code  ]);
		if( !$res ){
			return zy_json_echo( false , '状态更新失败 ，请稍候再试！ ');
		}
		return zy_json_echo( true , '状态更新成功！');
	}
	
	public function addRemark()
	{
		if( !$this->request->isPost()){
			return zy_json_echo( false , '请求类型错误！' );
		}
		$param = $this->request->param();
		$id = $param[ 'id' ];
		$remarks = $param [ 'remarks' ] ;
		$res = Db::name( 'system_update_record' )->where( 'id' , $id )->update( [ 'remarks' => $remarks ] );
		if( !$res ){
			return zy_json_echo( false , '数据保存失败 ！' );
		}
		return zy_json_echo( true , '数据保存成功！ ');
	}
	
	public function delete($id){
		$find = Db::name( 'system_update_record' )->where( 'id' , $id )->find();
		try{
			$path = ROOT_PATH.'public'.DS.$find[ 'source' ];
			if( file_exists($path) ){
				unlink( $path );
			}
			$res = Db::name('system_update_record')->where( 'id' , $id )->delete();
		}catch( \Exception $e ){
			return zy_json_echo( false , $e->getMessage());
		}
		if( !$res ){
			return zy_json_echo ( false , '删除失败，请稍候再试！' );
		}
		return zy_json_echo( true , '删除成功！' );
	}
}